using System.Collections;
using System.Collections.Generic;

namespace Kotonoha.Server.UI.Comparators {
    public class MasterComparator<T> : IComparer<T>, IComparer where T : class {
        private readonly List<IComparer<T>> comparers;
        private readonly int direction;

        public MasterComparator(List<IComparer<T>> comparers, bool reverse) {
            this.comparers = comparers;
            this.direction = reverse ? -1 : 1;
        }

        public int Compare(T x, T y) {
            foreach (var c in comparers) {
                int res = c.Compare(x, y);
                if (res == 0) {
                    continue;
                }
                return res*direction;
            }
            return 0;
        }

        public int Compare(object x, object y) {
            return Compare(x as T, y as T);
        }
    }
}